<template>
    <div class="quest-field">
        <div v-for="(i,n) in configData" :key="n">
            <div class="checkbox-field" v-if="i.ControlType==0" :class="{'van-cell--required':i.IsRequire}">
                <div class="left-label">{{i.ControlName}}</div>
                <van-checkbox-group v-model="i.ChooseValue">
                    <van-checkbox
                        shape="square"
                        v-for="(j, m) in i.ControlValue"
                        :key="m"
                        :name="j"
                    >
                        {{ j }}
                    </van-checkbox>
                </van-checkbox-group>
            </div>
            <div class="radio-field" v-if="i.ControlType==1" :class="{'van-cell--required':i.IsRequire}">
                <div class="left-label">{{i.ControlName}}</div>
                <van-radio-group v-model="i.ChooseValue">
                    <van-radio v-for="(j, m) in i.ControlValue" :key="m" :name="j">{{j}}</van-radio>
                </van-radio-group>
            </div>
            <div class="select-field" v-if="i.ControlType==2" :class="{'van-cell--required':i.IsRequire}">
                <div class="left-label">{{i.ControlName}}</div>
                <div class="right-select">
                    <select v-model="i.ChooseValue" @change="selectChange(i)">
                        <option :value="'请选择'+i.ControlName" disabled selected>请选择{{i.ControlName}}</option>
                        <option v-for="(j, m) in i.ControlValue" :key="m" :value="j">{{j}}</option>
                    </select>
                </div>    
            </div>
            <van-field
                v-if="i.ControlType==3"
                :class="{'active':focusIdx==i.ID}"
                v-model="i.ChooseValue"
                :label="i.ControlName"
                :required="i.IsRequire"
                :placeholder="'请输入'+i.ControlName"
                @focus="onFocus(i)"
                @blur="onBlur(i)"
            />
            <div class="textarea-field" v-if="i.ControlType==4" :class="{'van-cell--required':i.IsRequire}">
                <div class="textarea-label">{{i.ControlName}}</div>
                <div class="textarea-value" :class="{'active':focusIdx==i.ID}">
                    <textarea
                    v-model="i.ChooseValue"
                    :placeholder="'请输入'+i.ControlName"
                    maxlength="200"                     
                    @focus="onFocus(i)"
                    @blur="onBlur(i)"></textarea>
                    <!-- <div class="textarea-prompt">{{i.ChooseValue.length}}/150字</div> -->
                </div>        
            </div>
        </div>
    </div>
</template>

<script>
export default {
  props: ['config'],
  data() {
    return {
      configData: [],
      focusIdx: null,
      isError: false
    };
  },
  created() {
    this.config.data.forEach(item => {
      if (item.ControlType < 3 && item.ControlValue != null) {
          if (item.ControlType == 0) {
              item.ChooseValue = [];
          } else if (item.ControlType == 2) {
              item.ChooseValue = '请选择' + item.ControlName;
          } else {
              item.ChooseValue = '';
          }
        item.ControlValue = item.ControlValue.split(',');
      }
    });
    this.configData = this.config.data;
    var that = this;
    this.$store.commit('SET_QUEST', that.configData);
    console.log(this.$store.state.UserInfo.QuestionnaireItems);
  },
  methods: {
      selectChange(item) {
          console.log(item.ChooseValue);
      },
      onFocus(item) {
          this.focusIdx = item.ID;
      },
      onBlur(item) {
          this.focusIdx = null;
      }
  },
  watch: {
    configData: {
      handler: function(val, oldVal) {
        this.$store.commit('SET_QUEST', val);
      },
      deep: true
    }
  }
};
</script>

<style>
.checkbox-field,
.radio-field ,
.select-field{
  width: 100%;
  display: flex;
  position: relative;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  background-color: #fff;
  color: #333;
  font-size: 14px;
  padding: 0 10px 0 15px;
  margin: 0 0 10px 0;
  justify-content: space-between;
}
.checkbox-field > div,
.radio-field>div,
.select-field>div{
  flex: 1;
}
.left-label {
  max-width: 90px;
}
.checkbox-field .van-checkbox-group,
.radio-field .van-radio-group{
    display: flex;
    flex-wrap: wrap;
}
.checkbox-field .van-checkbox__icon--checked .van-icon{
    border-color: #37acff;
    background-color: #37acff;
}
.radio-field .van-radio .van-icon-checked{
    color: #37acff;
}
.radio-field .van-radio .van-icon-check{
    color: #e5e5e5;
}
.checkbox-field .van-checkbox,
.radio-field .van-radio{
    margin: 0 15px 5px 0;
}
.radio-field .van-radio__input{
    line-height: 1em;
}
.checkbox-field .van-checkbox__label,
.radio-field .van-radio__label{
    margin-left: 5px;
    font-size: .875em;
}
.select-field{
    align-items: center;
}
.right-select select{
    width: 100%;
    height: 39px;
    background: #fff;
    color: #4d4d4d;
    border: 1px solid #dcdfe6;
    padding: 0 10px;
    outline: none;
    border-radius: 4px;
    line-height: 39px;
    resize: none;
    display: block;
    box-sizing: border-box;
    appearance: none;
}
.right-select select option{
    background: #fff;
    color:#4d4d4d;
}
.right-select select option:hover{
    background: #37acff;
    color:#fff;
}
</style>
